package com.yawn;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;

import java.util.List;

/**
 * @author Created by yawn on 2017-12-09 13:31
 */
public class TestQuery {

    public static void main(String[] args) {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        IdentityService identityService = engine.getIdentityService();
        // list()
        List<Group> groupList = identityService.createGroupQuery().list();
        groupList.forEach(TestQuery::printGroup);

        // listPage()
        List<Group> groupList1 = identityService.createGroupQuery().listPage(3, 5);
        groupList1.forEach(TestQuery::printGroup);

        // desc() asc()
        List<Group> groupList2 = identityService.createGroupQuery().orderByGroupName().desc().list();
        /// 多字段查询
//        List<Group> groupList2 = identityService
//                .createGroupQuery()
//                .orderByGroupName().desc()
//                .orderByGroupType().asc()
//                .list();
        groupList2.forEach(TestQuery::printGroup);

        // GroupName
        Group group3 = identityService.createGroupQuery().groupName("Group_3").singleResult();
        printGroup(group3);
        // GroupNameLike
        Group group5 = identityService.createGroupQuery().groupNameLike("%p_5").singleResult();
        printGroup(group5);


        // native SQL 查询
        List<Group> groupList3 = identityService.createNativeGroupQuery()
                .sql("select * from act_id_group where NAME_ like #{name} order by ID_ desc")
                .parameter("name", "Group_%")
                .list();
        groupList3.forEach(TestQuery::printGroup);


        engine.close();
    }


    private static void printGroup(Group group) {
        System.out.println("Group { id=" + group.getId() + ", name=" + group.getName() + ", type=" + group.getType() + " }");
    }
}
